08. Roll a Yahtzee
Roll a Yahtzee
Question:
Start Quiz:
public int keepRolling() {
int dice1 = rollDice();
int dice2 = rollDice();
int dice3 = rollDice();
int count = 1;
while (!(dice1 == dice2 && dice2 == dice3)) {
//we need to re-roll
dice1 = rollDice();
dice2 = rollDice();
dice3 = rollDice();
count = count + 1;
}
return count;
}
Solution:
Quiz help
You should modify the function int keepRolling()
by adding code to check for a 5 dice match (Yahtzee) rather than just 3 dice.
To do so, follow these steps:
- Add 2 new integer variables
dice4
anddice5
and initialize them to the function callrollDice()
- Then inside the while loop, make sure you reset the values of those integers to another call of
rollDice()
each. - Finally, update the while loop condition to check for the mismatch of all 5 dice. Remember you can simply check that all dice match and surround that with the
!
not operator like with the existing 3 dice implementation.